﻿using gregn6Lib;
using NursingFormSystem;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using NursingFormSystem.NurFormsService;
using SQLDLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ShiftProject.Scale
{
    public partial class HierarchicalPrint : Form
    {
        #region 变量声明
        private GridppReport Report = new GridppReport();
        NursingFormRecord record = new NursingFormRecord();
        NursingForm formService = new NursingForm();
        PatientInfo PatientInfo;
        DateTime Start;
        DateTime End;
        #endregion

        #region 构造函数
        public HierarchicalPrint(PatientInfo patientInfo, DateTime StartDate, DateTime EndDate)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            Start = StartDate;
            End = EndDate;
            //载入报表模板数据
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\护理分级表.grf");
            //设置与数据源的连接串，因为在设计时指定的数据库路径是绝对路径。
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            //连接报表取数事件
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            //设定查询显示器关联的报表
            axGRPrintViewer1.Report = Report;
        }
        #endregion

        #region 连接报表取数事件
        private void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecordByDate(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.PatientCare, Start, End);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);

                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord rd = list[i];
                    NursingGrade ps = XmlUtil.XMLToObject<NursingGrade>(rd.NursingFormData, Encoding.UTF8);
                    //获取电签图片
                    //StringBuilder sbSealImageBase64 = new StringBuilder(iLength);//先开辟内存空间
                    string image = "";
                    string userName = ps.ZenRenHuShiQianMing;
                    if (!string.IsNullOrEmpty(userName))
                    {
                        image = GetElectricSignBase64(rd.AreaId, ps.ZenRenHuShiQianMing);
                    }
                    dt.Rows.Add(
                                PatientInfo.PATIENT_ID,//患者ID
                                PatientInfo.PATIENT_NAME,//患者姓名
                                PatientInfo.DEPARTMENT_NAME,//科室
                                PatientInfo.NURSE_CELL_NAME,//病区
                                PatientInfo.BED_NO,//床号
                                Convert.ToDateTime(rd.AssessmentDate),//评估日期

                                ps.JinShi == "0" ? "10" : "",
                                ps.JinShi == "1" ? "5" : "",
                                ps.JinShi == "2" ? "0" : "",
                                ps.XiZao == "0" ? "5" : "",
                                ps.XiZao == "1" ? "0" : "",
                                ps.XiuShi == "0" ? "5" : "",
                                ps.XiuShi == "1" ? "0" : "",
                                ps.ChuangYi == "0" ? "10" : "",
                                ps.ChuangYi == "1" ? "5" : "",
                                ps.ChuangYi == "2" ? "0" : "",
                                ps.KongZhiDaBian == "0" ? "10" : "",
                                ps.KongZhiDaBian == "1" ? "5" : "",
                                ps.KongZhiDaBian == "2" ? "0" : "",
                                ps.KongZhiXiaoBian == "0" ? "10" : "",
                                ps.KongZhiXiaoBian == "1" ? "5" : "",
                                ps.KongZhiXiaoBian == "2" ? "0" : "",
                                ps.RuCe == "0" ? "10" : "",
                                ps.RuCe == "1" ? "5" : "",
                                ps.RuCe == "2" ? "0" : "",
                                ps.ChuangYiZhuangYi == "0" ? "15" : "",
                                ps.ChuangYiZhuangYi == "1" ? "10" : "",
                                ps.ChuangYiZhuangYi == "2" ? "5" : "",
                                ps.ChuangYiZhuangYi == "3" ? "0" : "",
                                ps.PingDiXingZou == "0" ? "15" : "",
                                ps.PingDiXingZou == "1" ? "10" : "",
                                ps.PingDiXingZou == "2" ? "5" : "",
                                ps.PingDiXingZou == "3" ? "0" : "",
                                ps.ShangXiaLouTi == "0" ? "10" : "",
                                ps.ShangXiaLouTi == "1" ? "5" : "",
                                ps.ShangXiaLouTi == "2" ? "0" : "",
                                ps.ZiLiNengLi == "0" ? "√" : "",
                                ps.ZiLiNengLi == "1" ? "√" : "",
                                ps.ZiLiNengLi == "2" ? "√" : "",
                                ps.ZiLiNengLi == "3" ? "√" : "",
                                ps.BingQingDengJi == "0" ? "√" : "",
                                ps.BingQingDengJi == "1" ? "√" : "",
                                ps.BingQingDengJi == "2" ? "√" : "",
                                ps.HuLiJiBie,
                                !Base.ESSwitch.Equals("on") ? ps.ZenRenHuShiQianMing : "",
                                ps.ZongFen,
                                image
                                );
                }

                //将 DataTable 的数据转储到 Grid++Report 的数据集中
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);

            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }

        private static void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("PatientId", typeof(string));
            dt.Columns.Add("PatientName", typeof(string));
            dt.Columns.Add("DepartmentName", typeof(string));//科室
            dt.Columns.Add("AreaName", typeof(string));//病区            
            dt.Columns.Add("BedNo", typeof(string));//床号
            dt.Columns.Add("AssessmentDate1", typeof(DateTime));//评估日期

            dt.Columns.Add("Eat1", typeof(string));//进食：完全独立
            dt.Columns.Add("Eat2", typeof(string));//进食：需部分帮助
            dt.Columns.Add("Eat3", typeof(string));//进食：需极大帮助
            dt.Columns.Add("Bathe1", typeof(string));//洗澡：潮湿
            dt.Columns.Add("Bathe2", typeof(string));//洗澡：活动力
            dt.Columns.Add("Decorate1", typeof(string));//修饰：完全独立
            dt.Columns.Add("Decorate2", typeof(string));//修饰：需部分帮助
            dt.Columns.Add("Dressing1", typeof(string));//穿衣：完全独立
            dt.Columns.Add("Dressing2", typeof(string));//穿衣：需部分帮助
            dt.Columns.Add("Dressing3", typeof(string));//穿衣：需极大帮助
            dt.Columns.Add("ControlStool1", typeof(string));//控制大便：可控制
            dt.Columns.Add("ControlStool2", typeof(string));//控制大便：偶尔控制
            dt.Columns.Add("ControlStool3", typeof(string));//控制大便：完全失控
            dt.Columns.Add("ControlUrination1", typeof(string));//控制小便：可控制
            dt.Columns.Add("ControlUrination2", typeof(string));//控制小便：偶尔控制
            dt.Columns.Add("ControlUrination3", typeof(string));//控制小便：完全失控
            dt.Columns.Add("GoToilet1", typeof(string));//如厕：完全独立
            dt.Columns.Add("GoToilet2", typeof(string));//如厕：需部分帮助
            dt.Columns.Add("GoToilet3", typeof(string));//如厕：需极大帮助
            dt.Columns.Add("BedChairTransfer1", typeof(string));//床椅转移：完全独立
            dt.Columns.Add("BedChairTransfer2", typeof(string));//床椅转移：需部分帮助
            dt.Columns.Add("BedChairTransfer3", typeof(string));//床椅转移：需极大帮助
            dt.Columns.Add("BedChairTransfer4", typeof(string));//床椅转移：完全依赖
            dt.Columns.Add("LevelWalk1", typeof(string));//平地行走：完全独立
            dt.Columns.Add("LevelWalk2", typeof(string));//平地行走：需部分帮助
            dt.Columns.Add("LevelWalk3", typeof(string));//平地行走：需极大帮助
            dt.Columns.Add("LevelWalk4", typeof(string));//平地行走：完全依赖
            dt.Columns.Add("StairActivity1", typeof(string));//上下楼梯：完全独立
            dt.Columns.Add("StairActivity2", typeof(string));//上下楼梯：需部分帮助
            dt.Columns.Add("StairActivity3", typeof(string));//上下楼梯：需极大帮助
            dt.Columns.Add("SelfCareGrade1", typeof(string));//自理能力等级：重度依赖（<=40分）
            dt.Columns.Add("SelfCareGrade2", typeof(string));//自理能力等级：中度依赖（41-60分）
            dt.Columns.Add("SelfCareGrade3", typeof(string));//自理能力等级：轻度依赖（61-99分）
            dt.Columns.Add("SelfCareGrade4", typeof(string));//自理能力等级：无需依赖（100分）
            dt.Columns.Add("IllnessGrade1", typeof(string));//病情等级：病危/抢救
            dt.Columns.Add("IllnessGrade2", typeof(string));//病情等级：病重/病情不稳
            dt.Columns.Add("IllnessGrade3", typeof(string));//病情等级：病情稳定/康复期
            dt.Columns.Add("Nursing", typeof(string));//护理级别
            dt.Columns.Add("NurseSign", typeof(string));//责任护士签名
            dt.Columns.Add("sum", typeof(string));//总分
            dt.Columns.Add("ElectricSignBase64", typeof(string));//电签
        }
        #endregion

        #region 窗体加载启动锐浪报表
        private void HierarchicalPrint_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
        #endregion

        #region 解析电签图片
        private string GetElectricSignBase64(string DepartmentCode, string UserName)
        {
            string image = string.Empty;
            string esSwitch = Base.ESSwitch;
            if (!esSwitch.Equals("on")) return image;
            string sql = string.Format("SELECT * FROM P_Personnel(NOLOCK) WHERE DepartmentCode = '{0}' AND name='{1}'", DepartmentCode, UserName);
            DataSet ds = DLL.SqlHelper.ExecuteDataset(sql, CommandType.Text, SQLDLL.Base.ConnNISStr, null);
            if (ds == null || ds.Tables.Count <= 0) return image;
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count <= 0) return image;
            if (dt.Rows[0]["ElectricSignBase64"] == DBNull.Value) return image;
            image = dt.Rows[0]["ElectricSignBase64"].ToString();
            return image;
        }
        #endregion
    }
}